package org.phalanx.server.game;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import org.phalanx.map.MapCoordinates;
import org.phalanx.server.PhalanxServer;
import org.phalanx.server.ServerCache.AbstractCacheHandler;
import org.phalanx.server.ServerCache.Cache;
import org.phalanx.server.ServerCache.CacheManager;
import org.phalanx.server.ServerCache.Cacheable;
import org.phalanx.server.ServerCache.QueryObject;
import org.phalanx.server.sql.SQLAction;
import org.phalanx.server.sql.SQLManager;

public class PhalanxPlayerStateCacheHandler implements AbstractCacheHandler {
	public static final String tableName = "currentplayerstate";
	
	public static final String GAMEID = "gameid";
	public static final String USERID = "userid";
	public static final String WEALTH = "wealth";
	public static final String NATION_STATUS = "nationstatus";
	public static final String GAME_ALIAS = "gamealias";
	public static final String TEAM_COLOR = "teamcolor";
	public static final String BUILD_POINT = "buildpoint";
	

	@Override
	public boolean addElementToDB(Cache c, Object newKey, Cacheable newElement) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteObjectFromDB(Cache c, Object newKey,
			Cacheable newElement) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ArrayList<Cacheable> getMultipleObjectsFromDB(Cache c,
			Object newKey, boolean cacheUpdate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Cacheable getObjectFromDB(Cache c, Object newKey, boolean cacheUpdate) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean populateCache(Cache c) {
		boolean isSuccess = false;
		
		SQLManager sqlMan = PhalanxServer.getServerInstance().getSQLManager();
		ArrayList<QueryObject> results = new ArrayList<QueryObject>();
		
		CacheManager cacheMgr = PhalanxServer.getServerInstance().getCacheManager();

		QueryObject playerStateQuery = new QueryObject();
		playerStateQuery.setInteger(GAMEID, 0);
		playerStateQuery.setInteger(USERID, 0);
		playerStateQuery.setInteger(WEALTH, 0);
		playerStateQuery.setInteger(NATION_STATUS, 0);
		playerStateQuery.setString(GAME_ALIAS, "");
		playerStateQuery.setInteger(TEAM_COLOR, 0);
		playerStateQuery.setString(BUILD_POINT, "");
		
		SQLAction getAllUsers = new SQLAction(SQLAction.SELECT, tableName);
		getAllUsers.addField("*");
		
		try {
			results = sqlMan.getQuery(getAllUsers.getActionString(), playerStateQuery);
			
			for (int i = 0; i < results.size(); i++){
				QueryObject temp = results.get(i);
				
				Integer gameId = temp.getInteger(GAMEID);
				
				PhalanxGameSettings gameSettings = (PhalanxGameSettings)cacheMgr.getElement("GameListCache", gameId);
				
				if (gameSettings != null){
					PhalanxPlayerState pState;
					
					if (c.get(gameId) != null){
						pState = (PhalanxPlayerState)c.get(gameId);
					}else {
						pState = new PhalanxPlayerState(gameId);
						c.put(gameId, pState);
					}
					
					PhalanxGamePlayer player = new PhalanxGamePlayer();
					player.setUserId(temp.getInteger(USERID));
					player.setWealth(temp.getInteger(WEALTH));
					player.setNationstatus(temp.getInteger(NATION_STATUS));
					player.setGameAlias(temp.getString(GAME_ALIAS));
					player.setTeamColor(temp.getInteger(TEAM_COLOR));
					player.setBuildPoint(getMapCoordinates(temp.getString(BUILD_POINT)));
					
					pState.addPlayerToList(player);
				} // gamesettings != null
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		return isSuccess;
	}
	
	private MapCoordinates getMapCoordinates(String coorStr){
		MapCoordinates c = null;
		
		if (coorStr.length() >= 5){
			coorStr = coorStr.substring(1, coorStr.length() - 1);
			
			String[] xyCoor = coorStr.split(",");
			
			if (xyCoor.length > 1){
				c = new MapCoordinates(Integer.valueOf(xyCoor[0]), Integer.valueOf(xyCoor[1]));
			}
		}
		
		return c;
	}

	@Override
	public boolean updateElementToDB(Cache c, Object newKey,
			Cacheable newElement) {
		// TODO Auto-generated method stub
		return false;
	}

}
